const prefix = 'h5_pf_'

const rules = {
  address: [
    { required: true, message: '请输入地址', trigger: 'blur' },
  ],
  usdtplace: [
    { required: true, message: '请输入USDT地址', trigger: 'blur' },
  ],
  title: [
    { required: true, message: '请输入标题', trigger: 'blur' },
  ],
  contents: [
    { required: true, message: '请输入内容', trigger: 'blur' },
  ],
  connect: [
    { required: true, message: '请输入联系方式', trigger: 'blur' },
  ],
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' },
  ],
  mobile: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^[1]([3-9])[0-9]{9}$/, message: '请输入正确的手机号', trigger: 'blur' },
  ],
  captcha: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
  ],
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  oldpassword: [
    { required: true, message: '请输入旧密码', trigger: 'blur' },
  ],
  newpassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
  ],
  newpassword_b: [
    { required: true, message: '请输入确认新密码', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ],
  tjcode: [
    { required: true, message: '请输入推荐人账号', trigger: 'blur' },
  ],
  paypwd: [
    { required: true, message: '请输入支付密码', trigger: 'blur' },
    { len: 6, message: '请输入6位支付密码', trigger: 'blur' },
  ],
  paypwd_b: [
    { required: true, message: '请确认支付密码', trigger: 'blur' },
  ],
  newpaypwd: [
    { required: true, message: '请输入支付密码', trigger: 'blur' },
    { len: 6, message: '请输入6位支付密码', trigger: 'blur' },
  ],
  newpaypwd_b: [
    { required: true, message: '请确认支付密码', trigger: 'blur' },
  ],
  resource: [
    { required: true, message: '请选择活动资源', trigger: 'change' }
  ],
  desc: [
    { required: true, message: '请填写活动形式', trigger: 'blur' }
  ]
}

const $http = {
  async get(url, params = {}) {
    let res = await axios.get(url, { params }).catch(error => {
      console.log(error.response)
      if(error.response.status === 401) {
        window.location.href = './login.html'
      }
    })
    return res
  },
  async post (url, params) {
    let config = {
      headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' }
    }

    let res = await axios.post(url, Qs.stringify(params, { allowDots: true }), config).catch(error => {
      if(error.response.status === 401) {
        window.location.href = './login.html'
      }
    })

    return res
  },
  async upload(url, formData) {
    
    let config = {
      headers: { 'Content-Type': 'multipart/form-data;' }
    }

    let res = await axios.post(url, formData, config)
    return res
  }
}

let u = navigator.userAgent;
let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;   //判断是否是 android终端
let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);     //判断是否是 iOS终端  
let isSend = false

const $utils = {
  isAndroid,
  isIOS,
  // 获取验证码
  getLocalStorage(key) {
    return localStorage.getItem(prefix + key)
  },
  setLocalStorage(key, value) {
    return localStorage.setItem(prefix + key, value)
  },
  query(params) {
    let search = location.search.slice(1)
    let obj = Qs.parse(search)
    return obj[params]
  },
  drawPic(container, canvas) {
     /**生成一个随机数**/
     function randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    }

    let code = ''; //首先默认code为空字符串
    if (!canvas) {
      $('body').append('<canvas style="display:none" id="canvas" width="106" height="41"></canvas>')
      canvas = document.getElementById("canvas")
    }

    let width = canvas.width;
    let height = canvas.height;
    let ctx = canvas.getContext('2d');
    ctx.textBaseline = 'bottom';
    canvas.style.display = 'none'
    ctx.clearRect(0, 0, width, height)
    /**绘制背景色**/
    ctx.fillStyle = '#fff';
    ctx.fillRect(0, 0, width, height);
    /**绘制文字**/
    var str = '0123456789';
    for (var i = 0; i < 4; i++) {
      var txt = str[randomNum(0, str.length)];
      ctx.fillStyle = "#0007F6"; // 字体颜色
      ctx.font = 'bold 28px MicrosoftYaHei'; // 字体大小
      var x = 15 + i * 17;
      var y = randomNum(35, 40);
      var deg = randomNum(-10, 10);
      //修改坐标原点
      ctx.translate(x, y);
      ctx.rotate(deg * Math.PI / 180);
      ctx.fillText(txt, 0, 0);
      //恢复坐标原点和旋转角度
      ctx.rotate(-deg * Math.PI / 180);
      ctx.translate(-x, -y);
      code = code + txt;
    };
    //新建Image对象，将canvas转换为base64添加到image的src属性上
    var img = new Image();
    img.onload = function () { ctx.drawImage(img, 0, 0); }
    img.src = canvas.toDataURL('image/jpg');
    //添加到body中
    container.html(img)
    return code
  },
  showMsg(content = '', callback?: any) {
    return layer && layer.open({
      content: content,
      skin: 'msg',
      time: 2,
      success: function () {
        callback && setTimeout(callback, 2000)
      }
    });
  },
  sendSms($el) {
    if(isSend) {
      return isSend;
    }

    isSend = true
    
    let text = $el.text()
    let i = 60
    $el.text(i +'s')
    let timer = setInterval(() => {
      i--;
      $el.text(i + 's')
      
      if(i === 0) {
        clearInterval(timer)
        $el.text(text)
        isSend = false
      }
    }, 1000);

  },
  getPathElement(e, className) {
    for(let item of e.path) {
      if(item.className === className) {
        return item
      }
    }
  }
}

